package com.hospital.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hospital.configure.Result;
import com.hospital.entity.NucleicAcid;
import com.hospital.mapper.NucleicAcidMapper;
import com.hospital.service.NucleicAcidService;
import com.hospital.utils.IDGenerator;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author WuLiting
 * @since 2021-03-24
 */
@Service
public class NucleicAcidServiceImpl extends ServiceImpl<NucleicAcidMapper, NucleicAcid> implements NucleicAcidService {

    @Resource
    NucleicAcidMapper nucleicAcidMapper;

    @Override
    public Result<NucleicAcid> updateNucleicAcid(NucleicAcid nucleicAcid) {
        Result<NucleicAcid> result = new Result<>();
        try {

            nucleicAcid.setTestTime(new Date());

            QueryWrapper<NucleicAcid> wrapper = new QueryWrapper<>();
            wrapper.lambda().eq(NucleicAcid::getUserId,nucleicAcid.getUserId());

            List<NucleicAcid> nucleicAcids = nucleicAcidMapper.selectList(wrapper);
            if(nucleicAcids != null && nucleicAcids.size() != 0){
                nucleicAcidMapper.update(nucleicAcid,wrapper);
            }else{
                nucleicAcid.setNucleicAcidId(IDGenerator.getUUID("NUCLEIC"));
                nucleicAcidMapper.insert(nucleicAcid);
            }

            result.setMessage("success");
        } catch (Exception e) {
            result.setMessage(e.getMessage());
        }
        return result;
    }

    @Override
    public Result<NucleicAcid> delNucleicAcid(NucleicAcid nucleicAcid) {
        Result<NucleicAcid> result = new Result<>();
        try {
            if (StringUtils.isEmpty(nucleicAcid.getNucleicAcidId())) {
                result.setMessage("nucleicAcidId can't be empty");
                return result;
            }
            nucleicAcidMapper.deleteById(nucleicAcid.getNucleicAcidId());
            result.setMessage("success");
        } catch (Exception e) {
            result.setMessage(e.getMessage());
        }
        return result;
    }

    @Override
    public Result<NucleicAcid> batchDelNucleicAcid(NucleicAcid nucleicAcid) {
        Result<NucleicAcid> result = new Result<>();
        try {
            if (!StringUtils.isEmpty(nucleicAcid.getIds())) {
                String[] ids = nucleicAcid.getIds().split(",");
                for (String id : ids) {
                    nucleicAcidMapper.deleteById(id);
                }
            }
        } catch (Exception e) {
            result.setMessage(e.getMessage());
        }
        return result;
    }

    @Override
    public Result<NucleicAcid> queryNucleicAcid(NucleicAcid nucleicAcid) {
        Result<NucleicAcid> result = new Result<>();
        try {
            QueryWrapper<NucleicAcid> wrapper = new QueryWrapper<>();
            //查询条件拼接，仅限单表查询可用


            List<NucleicAcid> nucleicAcids = nucleicAcidMapper.selectList(wrapper);
            result.setReturnList(nucleicAcids);
            result.setMessage("success");
        } catch (Exception e) {
            result.setMessage(e.getMessage());
        }
        return result;
    }

    @Override
    public Result queryNucleicAcidByPage(NucleicAcid nucleicAcid) {
        Result<NucleicAcid> result = new Result<>();

        try {
            if (nucleicAcid.getCurrent() == null || nucleicAcid.getSize() == null) {
                result.setMessage("current or size can't be null");
                return result;
            }

            IPage<NucleicAcid> nucleicAcids = nucleicAcidMapper.queryNucleicAcid(new Page<>(nucleicAcid.getCurrent(), nucleicAcid.getSize()), nucleicAcid);
            result.setReturnPage(nucleicAcids);
            result.setMessage("success");
        } catch (Exception e) {
            result.setMessage(e.getMessage());
        }
        return result;
    }

    @Override
    public Result<NucleicAcid> queryNucleicAcidById(NucleicAcid nucleicAcid) {
        Result<NucleicAcid> result = new Result<>();
        try {
            NucleicAcid returnNucleicAcid = nucleicAcidMapper.selectById(nucleicAcid.getNucleicAcidId());
            result.setReturnObj(returnNucleicAcid);
            result.setMessage("success");
        } catch (Exception e) {
            result.setMessage(e.getMessage());
        }
        return result;
    }

}
